<?php

class Silk_Object_Router extends Zend_Controller_Router_Rewrite {

    public function route(Zend_Controller_Request_Abstract $pRequest){

 	$request = parent::route($pRequest);

	$url = $request->getRequestUri();

	if (preg_match('/\?.*/', $url))
	    $url = preg_replace('/\?.*/', '', $url);

// 	logdebug("Silk_Object_Router.route: Processing $url params=" . $_SERVER['QUERY_STRING']);

	$site = Silk::getCurrentSite();

	$Objects = new Silk_Objects();
	$object  = $Objects->fetchByRoute(rtrim(ltrim($url, '/'), '/'), $site->site_id);

	if ($object){
	    logdebug("Silk_Object_Router.route: Object [{$object->object_id}] matches route");
	    $request->setParam('object', $object);

	    $params = explode('&', $_SERVER['QUERY_STRING']);
	    foreach ($params as $param){
		$parts = explode('=', $param);
		if ($parts[0] == 'action')
		    $parts[0] = 'template';
		$request->setParam($parts[0], $parts[1]);
	    }

	    $request->setControllerName('object');
	    $request->setParam('controller', 'object');
	    $request->setActionName('render');
	    $request->setParam('action', 'render');
	}


// 	logdebug("Silk_Object_Router.route: Request=" . logobject($request));

	return $request;

    }

}

?>